package com.group.three.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group.three.pojo.Comment;
import com.group.three.pojo.vo.JsonResult;
import com.group.three.service.CommentService;
import com.group.three.utils.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 首次进入电影界面的分页
     * @param modelAndView
     * @return
     */
//    @RequestMapping("/toComment/{movieName}")
//    public ModelAndView toComment(ModelAndView modelAndView,
//                                  @PathVariable(value = "movieName") String movieName) {
//        PageInfo<Comment> homePageInfo =
//                commentService.selectHomeCommentList(movieName, 1, 3);
//        modelAndView.addObject("homePageInfo", homePageInfo);
//        modelAndView.setViewName("/2-movie/play");
//        return modelAndView;
//    }

    /**
     * 前台电影页面下留言分页
     * @param movieName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/homeCommentListPage/{movieName}")
    @ResponseBody
    public JsonResult homeCommentListPage(
            @PathVariable(value = "movieName") String movieName,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        PageInfo<Comment> homePageInfo =
                commentService.selectHomeCommentList(movieName, pageNum, pageSize);
        JsonResult jsonResult = new JsonResult("200", "前端分页成功", homePageInfo);
        return jsonResult;
    }

    /**
     * 用户留言上传
     * @param personId 用户ID
     * @param commentContent 留言内容
     * @param movieName 电影名
     * @param addTime 留言时间
     * @return
     */
    @RequestMapping("/insertComment")
    @ResponseBody
    public JsonResult insertComment(
            @RequestParam(value = "movieId") String movieId,
            @RequestParam(value = "commentNum") Integer commentNum,
            @RequestParam(value = "personId") String personId,
                                    @RequestParam(value = "commentContent") String commentContent,
                                    @RequestParam(value = "movieName") String movieName,
                                    @RequestParam(value = "addTime") String addTime) {
        commentService.updateAdminComment(Long.parseLong(movieId),commentNum);
        String s = String.valueOf(SnowFlakeUtil.getSnowFlakeId());
        String substring = s.substring(0, s.length() - 3);
        int i = commentService.insertComment(Long.parseLong(substring),Long.parseLong(personId), commentContent, movieName, addTime);
        if (i == 1) {
            JsonResult jsonResult = new JsonResult("200", "提交留言成功", i);
            return jsonResult;
        } else {
            JsonResult jsonResult = new JsonResult("400", "提交留言失败");
          return jsonResult;
        }
    }

    /**
     * 首次进入后台评论的列表
     * @param modelAndView
     * @return
     */
    @RequestMapping("/homeCommentPage")
    public ModelAndView homeCommentPage(ModelAndView modelAndView) {
        List<Comment> commentAll = commentService.selectCommentAll();
        PageInfo<Comment> pageInfo =
                commentService.selectCommentKeyWordList(null, 1, 6);
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.setViewName("/3-admin/comment_list");
        return modelAndView;
    }

    /**
     * 评论列表的分页
     *
     * @param modelAndView
     * @param keyWord      关键词
     * @param pageNum      页码
     * @param pageSize     一页多少行信息
     * @return
     */
    @RequestMapping("/homeComment")
    @ResponseBody
    public JsonResult homeComment(ModelAndView modelAndView,
                                  @RequestParam(value = "keyWord", required = false) String keyWord,
                                  @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", required = false, defaultValue = "6") Integer pageSize) {

        PageInfo<Comment> pageInfo = commentService.selectCommentKeyWordList(keyWord, pageNum, pageSize);
        return new JsonResult("200", "success", pageInfo);
    }

    /**
     * 通过commentId删除评论
     *
     * @param commentId
     * @return
     */
    @RequestMapping("/deleteComment")
    @ResponseBody
    public JsonResult deleteComment(@RequestParam(value = "commentId") String commentId,
                                    @RequestParam(value = "movieName") String movieName,
                                    @RequestParam(value = "movieId") Long movieId) {
        System.out.println("commentId = " + commentId);
        int i = commentService.deleteComment(Long.parseLong(commentId));
        PageInfo<Comment> pageInfo = commentService.selectCommentKeyWordList(movieName, 1, 6);
        long total = pageInfo.getTotal();
        String s = String.valueOf(total);
        //删除后对数据库中的评论进行更新
        commentService.updateAdminComment(movieId, Integer.parseInt(s));
        JsonResult jsonResult = new JsonResult("200", "删除成功");
        return jsonResult;
    }

    /**
     * 首次进入个人中心留言板
     * @param modelAndView
     * @param personName   用户昵称
     * @return
     */
    @RequestMapping("/adminComment/{personName}")
    public ModelAndView adminComment(ModelAndView modelAndView,
                                     @PathVariable(value = "personName") String personName
    ) {
        PageInfo<Comment> pageInfo = commentService.selectAdminCommentList(personName, 1, 5);
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.addObject("personName",personName);
        modelAndView.setViewName("/2-movie/comments");
        return modelAndView;
    }

    /**
     * 个人中心留言分页
     *
     * @param personName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/adminCommentList/{personName}")
    @ResponseBody
    public JsonResult adminCommentList(
            @PathVariable(value = "personName") String personName,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize
    ) {
        PageInfo<Comment> pageInfo = commentService.selectAdminCommentList(personName, pageNum, pageSize);
        JsonResult jsonResult = new JsonResult("200", "个人中心分页成功", pageInfo);
        return jsonResult;
    }



}
